﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extensions;
using DevTeam;
using Aker.DevTeam;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors;

namespace Aker.MainForms
{
    public partial class Task : XForm
    {
        public Task()
        {
            InitializeComponent();
        }
 
        private void Task_Load(object sender, EventArgs e)
        {  
            Data.TAM.TaskTableAdapter.Fill(Data.DB.Task);
            
            Data.TAM.KullaniciTableAdapter.Fill(Data.DB.Kullanici);
            BindingSource bs = new BindingSource(Data.DB , "Task");

            gv.Init(bs);
            taskDetail.DataBindings.Add("HtmlText", bs, "Task", true, DataSourceUpdateMode.OnPropertyChanged);

            gv.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gv.MakeColumnInvisible("Task");            
            gv.SetCaptions("TaskId:İş No.","InsertedBy:Ekleyen","InsertDate:Eklenme Tarihi","Status:Durum");
            Data.DB.Task.TableNewRow += Task_TableNewRow;
            gv.CustomColumnDisplayText += gv_CustomColumnDisplayText;
   
            gv.BestFitColumns();

            this.Shown += Task_Shown;
            this.FormClosing += Task_FormClosing;
            gv.FocusedRowChanged += gv_FocusedRowChanged;

        }

        void gv_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            Save();
        }

        void Task_Shown(object sender, EventArgs e)
        {
            Aker.DevTeam.Data.DB.Task.AcceptChanges();
        }

        void Task_FormClosing(object sender, FormClosingEventArgs e)
        {
            Save();
        }

        private void Save()
        {
            gv.EndEditing();
            bool changed = Data.DB.Task.HasChanged();
            if (changed)
            {
                int count = Data.TAM.TaskTableAdapter.Update(Data.DB.Task);
                //XtraMessageBox.Show(count + " satır kaydedildi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
  
        void gv_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "InsertedBy")
            {
                int userId = e.Value.ToIntNN();
                if (userId > -1)
                    e.DisplayText = Data.DB.Kullanici.FindByKullaniciId(userId).AdSoyad;
            }
        }

        void Task_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["InsertDate"] = DateTime.Now;
            e.Row["InsertedBy"] = AppSession.UserInfo.UserId;
            e.Row["Status"] = "Yeni";
        } 

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Save();
        }
    }
}
